#include "StdAfx.h"
#include ".\handler_fromworldserver.h"
#include ".\WorldServerQuery.h"
#include <PacketStruct_DW.h>
#include "ServerSession.h"


Handler_FromWorldServer_IMPL( DW_FRIEND_GETLIST_SYN )
{
	MSG_DW_FRIEND_GETLIST_SYN * pRecvMsg = (MSG_DW_FRIEND_GETLIST_SYN *)pMsg;

	TCHAR szQueryBuff[64];
	_sntprintf( szQueryBuff, sizeof(szQueryBuff), "{?=call S_Friend_SelectList(%d)}" , pRecvMsg->m_CharGuid );

	Query_Friend_SelectList * pQuery = Query_Friend_SelectList::ALLOC();
	pQuery->SetCharGuid( pRecvMsg->m_CharGuid );

	pQuery->SetQuery(szQueryBuff);
	pQuery->SetIndex( MAKEDWORD( (WORD)DW_FRIEND, (WORD)DW_FRIEND_GETLIST_DBR ) );
	pQuery->SetVoidObject( pServerSession );
	pServerSession->DBQuery( pQuery );
}

Handler_FromWorldServer_IMPL( DW_FRIEND_GETLIST_DBR )
{
	MSG_DBPROXY_RESULT * pResult		= (MSG_DBPROXY_RESULT *)pMsg;
	Query_Friend_SelectList * pQResult	= (Query_Friend_SelectList *)pResult->m_pData;

	if( !pQResult->ResultSuccess() )
	{
		MSG_DW_FRIEND_GETLIST_NAK nmsg;
		nmsg.m_CharGuid	= pQResult->GetCharGuid();
		nmsg.m_byResult	= 1;
		pServerSession->SendPacket( &nmsg, sizeof(nmsg) );

		Query_Friend_SelectList::FREE(pQResult); pQResult = NULL;
		return;
	}

	
	MSG_DW_FRIEND_GETLIST_ACK msg;
	msg.m_CharGuid		= pQResult->GetCharGuid();
	msg.m_byFriendNum	= (BYTE)pQResult->GetResultRowNum();
	ASSERT( pQResult->GetResultRowNum() < MAX_FRIENDBLOCK_STAT_NUM );
	for( int i = 0 ; i < msg.m_byFriendNum ; ++i )
	{
		msg.m_sFriendInfo[i].dwFriendGuid	= pQResult->pResult[i].m_FriendCharGuid;
		memcpy( msg.m_sFriendInfo[i].ptszFriendName, pQResult->pResult[i].m_ptszFriendCharName, MAX_CHARNAME_LENGTH*sizeof(TCHAR) );
		msg.m_sFriendInfo[i].byClass		= pQResult->pResult[i].m_byClass;
		msg.m_sFriendInfo[i].Level			= pQResult->pResult[i].m_Level;
		msg.m_sFriendInfo[i].Sts			= pQResult->pResult[i].m_Sts;
	}

	pServerSession->SendPacket( &msg, msg.GetSize() );
	Query_Friend_SelectList::FREE(pQResult); pQResult = NULL;
}

Handler_FromWorldServer_IMPL( DW_FRIEND_ADD_SYN )
{
	MSG_DW_FRIEND_ADD_SYN * pRecvMsg = (MSG_DW_FRIEND_ADD_SYN *)pMsg;

	TCHAR szQueryBuff[64];
	TCHAR tszTmpCharName[MAX_CHARNAME_LENGTH+1];
	memcpy( tszTmpCharName, pRecvMsg->m_ptszCharNameTo, MAX_CHARNAME_LENGTH*sizeof(TCHAR) );
	tszTmpCharName[MAX_CHARNAME_LENGTH]='\0';
	_sntprintf( szQueryBuff, sizeof(szQueryBuff), "{?=call S_Friend_Request(%d,'%s')}" , pRecvMsg->m_CharGuidFrom, tszTmpCharName );

	Query_Friend_Request * pQuery = Query_Friend_Request::ALLOC();
	pQuery->SetCharGuid( pRecvMsg->m_CharGuidFrom );

	pQuery->SetQuery(szQueryBuff);
	pQuery->SetIndex( MAKEDWORD( (WORD)DW_FRIEND, (WORD)DW_FRIEND_ADD_DBR ) );
	pQuery->SetVoidObject( pServerSession );
	pServerSession->DBQuery( pQuery );
}

Handler_FromWorldServer_IMPL( DW_FRIEND_ADD_DBR )
{
	MSG_DBPROXY_RESULT * pResult		= (MSG_DBPROXY_RESULT *)pMsg;
	Query_Friend_Request * pQResult	= (Query_Friend_Request *)pResult->m_pData;

	if( pQResult->ResultCode() != Query_Friend_Request::RETURNCODE_SUCCESS )
	{
		MSG_DW_FRIEND_ADD_NAK nmsg;
		nmsg.m_CharGuidFrom	= pQResult->GetCharGuid();
		nmsg.m_byResult		= pQResult->ResultCode();
		pServerSession->SendPacket( &nmsg, sizeof(nmsg) );

		Query_Friend_Request::FREE(pQResult); pQResult = NULL;
		return;
	}


	MSG_DW_FRIEND_ADD_ACK msg;
	msg.m_CharGuidFrom				= pQResult->GetCharGuid();
	msg.m_ToFriendInfo.dwFriendGuid	= pQResult->pResult[0].m_FriendCharGuid;
	memcpy( msg.m_ToFriendInfo.ptszFriendName, pQResult->pResult[0].m_ptszFriendCharName, MAX_CHARNAME_LENGTH*sizeof(TCHAR) );
	msg.m_ToFriendInfo.byClass		= pQResult->pResult[0].m_byClass;
	msg.m_ToFriendInfo.Level		= pQResult->pResult[0].m_Level;
	msg.m_ToFriendInfo.Sts			= 0;

	pServerSession->SendPacket( &msg, sizeof(msg) );
	Query_Friend_Request::FREE(pQResult); pQResult = NULL;
}

Handler_FromWorldServer_IMPL( DW_FRIEND_DEL_SYN )
{
	MSG_DW_FRIEND_DEL_SYN * pRecvMsg = (MSG_DW_FRIEND_DEL_SYN *)pMsg;

	TCHAR szQueryBuff[64];
	_sntprintf( szQueryBuff, sizeof(szQueryBuff), "{?=call S_Friend_Delete(%d,%d)}" , pRecvMsg->m_CharGuidFrom, pRecvMsg->m_CharGuidTo );

	Query_Friend_Delete * pQuery = Query_Friend_Delete::ALLOC();
	pQuery->SetCharGuid( pRecvMsg->m_CharGuidFrom );
	pQuery->SetCharGuidTo( pRecvMsg->m_CharGuidTo );

	pQuery->SetQuery(szQueryBuff);
	pQuery->SetIndex( MAKEDWORD( (WORD)DW_FRIEND, (WORD)DW_FRIEND_DEL_DBR ) );
	pQuery->SetVoidObject( pServerSession );
	pServerSession->DBQuery( pQuery );
}

Handler_FromWorldServer_IMPL( DW_FRIEND_DEL_DBR )
{
	MSG_DBPROXY_RESULT * pResult		= (MSG_DBPROXY_RESULT *)pMsg;
	Query_Friend_Delete * pQResult		= (Query_Friend_Delete *)pResult->m_pData;

	if( !pQResult->ResultSuccess() )
	{
		MSG_DW_FRIEND_DEL_NAK nmsg;
		nmsg.m_CharGuidFrom	= pQResult->GetCharGuid();
		nmsg.m_byResult		= 1;
		pServerSession->SendPacket( &nmsg, sizeof(nmsg) );

		Query_Friend_Delete::FREE(pQResult); pQResult = NULL;
		return;
	}


	MSG_DW_FRIEND_DEL_ACK msg;
	msg.m_CharGuidFrom				= pQResult->GetCharGuid();
	msg.m_CharGuidTo				= pQResult->GetCharGuidTo();
	pServerSession->SendPacket( &msg, sizeof(msg) );
	Query_Friend_Delete::FREE(pQResult); pQResult = NULL;
}

Handler_FromWorldServer_IMPL( DW_FRIEND_BLOCK_SYN )
{
	MSG_DW_FRIEND_BLOCK_SYN * pRecvMsg = (MSG_DW_FRIEND_BLOCK_SYN *)pMsg;

	TCHAR szQueryBuff[64];
	TCHAR tszTmpCharName[MAX_CHARNAME_LENGTH+1];
	memcpy( tszTmpCharName, pRecvMsg->m_ptszCharNameTo, MAX_CHARNAME_LENGTH*sizeof(TCHAR) );
	tszTmpCharName[MAX_CHARNAME_LENGTH]='\0';
	_sntprintf( szQueryBuff, sizeof(szQueryBuff), "{?=call S_Friend_BlockChar(%d,'%s')}" , pRecvMsg->m_CharGuidFrom, tszTmpCharName );

	Query_Friend_BlockChar * pQuery = Query_Friend_BlockChar::ALLOC();
	pQuery->SetCharGuid( pRecvMsg->m_CharGuidFrom );
	pQuery->SetCharNameTo( pRecvMsg->m_ptszCharNameTo );

	pQuery->SetQuery(szQueryBuff);
	pQuery->SetIndex( MAKEDWORD( (WORD)DW_FRIEND, (WORD)DW_FRIEND_BLOCK_DBR ) );
	pQuery->SetVoidObject( pServerSession );
	pServerSession->DBQuery( pQuery );
}


Handler_FromWorldServer_IMPL( DW_FRIEND_BLOCK_DBR )
{
	MSG_DBPROXY_RESULT * pResult		= (MSG_DBPROXY_RESULT *)pMsg;
	Query_Friend_BlockChar * pQResult	= (Query_Friend_BlockChar *)pResult->m_pData;

	if( !pQResult->ResultSuccess() )
	{
		MSG_DW_FRIEND_BLOCK_NAK nmsg;
		nmsg.m_CharGuidFrom	= pQResult->GetCharGuid();
		nmsg.m_byResult		= 1;
		pServerSession->SendPacket( &nmsg, sizeof(nmsg) );

		Query_Friend_BlockChar::FREE(pQResult); pQResult = NULL;
		return;
	}

	MSG_DW_FRIEND_BLOCK_ACK msg;
	msg.m_CharGuidFrom				= pQResult->GetCharGuid();
	msg.m_BlockInfo.dwBlockGuid		= pQResult->pResult[0].m_BlockedCharGuid;
	memcpy( msg.m_BlockInfo.ptszBlockName, pQResult->GetCharNameTo(), MAX_CHARNAME_LENGTH*sizeof(TCHAR) );
	pServerSession->SendPacket( &msg, sizeof(msg) );
	Query_Friend_BlockChar::FREE(pQResult); pQResult = NULL;
}


Handler_FromWorldServer_IMPL( DW_FRIEND_BLOCK_FREE_SYN )
{
	MSG_DW_FRIEND_BLOCK_FREE_SYN * pRecvMsg = (MSG_DW_FRIEND_BLOCK_FREE_SYN *)pMsg;

	TCHAR szQueryBuff[64];
	TCHAR tszTmpCharName[MAX_CHARNAME_LENGTH+1];
	memcpy( tszTmpCharName, pRecvMsg->m_ptszCharNameTo, MAX_CHARNAME_LENGTH*sizeof(TCHAR) );
	tszTmpCharName[MAX_CHARNAME_LENGTH]='\0';
	_sntprintf( szQueryBuff, sizeof(szQueryBuff), "{?=call S_Friend_FreeChar(%d,'%s')}" , pRecvMsg->m_CharGuidFrom, tszTmpCharName );

	Query_Friend_FreeChar * pQuery = Query_Friend_FreeChar::ALLOC();
	pQuery->SetCharGuid( pRecvMsg->m_CharGuidFrom );
	pQuery->SetCharNameTo( pRecvMsg->m_ptszCharNameTo );

	pQuery->SetQuery(szQueryBuff);
	pQuery->SetIndex( MAKEDWORD( (WORD)DW_FRIEND, (WORD)DW_FRIEND_BLOCK_FREE_DBR ) );
	pQuery->SetVoidObject( pServerSession );
	pServerSession->DBQuery( pQuery );

}

Handler_FromWorldServer_IMPL( DW_FRIEND_BLOCK_FREE_DBR )
{
	MSG_DBPROXY_RESULT * pResult		= (MSG_DBPROXY_RESULT *)pMsg;
	Query_Friend_FreeChar * pQResult	= (Query_Friend_FreeChar *)pResult->m_pData;

	if( !pQResult->ResultSuccess() )
	{
		MSG_DW_FRIEND_BLOCK_FREE_NAK nmsg;
		nmsg.m_CharGuidFrom	= pQResult->GetCharGuid();
		nmsg.m_byResult		= 1;
		pServerSession->SendPacket( &nmsg, sizeof(nmsg) );

		Query_Friend_FreeChar::FREE(pQResult); pQResult = NULL;
		return;
	}

	MSG_DW_FRIEND_BLOCK_FREE_ACK msg;
	msg.m_CharGuidFrom			= pQResult->GetCharGuid();
	memcpy( msg.m_ptszCharNameTo, pQResult->GetCharNameTo(), MAX_CHARNAME_LENGTH*sizeof(TCHAR) );
	pServerSession->SendPacket( &msg, sizeof(msg) );
	Query_Friend_FreeChar::FREE(pQResult); pQResult = NULL;
}

